home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3384 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: SPOD2.dev.esoc.esa.de!328PT
  2. From: 328pt@SPOD2.dev.esoc.esa.de (Phil Tregoning)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Please help ?!
  5. Date: 28 Jan 1996 15:59:36 GMT
  6. Organization: European Space Operations Center
  7. Message-ID: <4eg6h8$1db5@info.estec.esa.nl>
  8. References: <4dm889$3hs@neptunus.pi.net> <4drnv1$cr@news.iag.net> <4drq5i$cr@news.iag.net> <4e6hse$dvl@ns.RezoNet.NET> <310a2389.49571776@nntp.ix.netcom.com>
  9. Reply-To: 328pt@SPOD2.dev.esoc.esa.de
  10. NNTP-Posting-Host: spod2.dev.esoc.esa.de
  11.  
  12. >> In referenced article, John R Buchan says...
  13. >>
  14. >> >>   cpy = (char *) malloc(MAXLEN);
  15. >> >
  16. >> >The cast is unnecessary and can hide errors.  You should remove it.
  17. >ray@ultimate-tech.com (Ray Dunn) wrote:
  18. >
  19. >> [...] adding a cast to a pointer of the same 
  20. >> type to the malloc return is the *safest* thing you can do:
  21. >> 
  22. >>   fred = malloc(n * sizeof(int));
  23. >> 
  24. >> Oops - fred isn't an "int *" it's a "long *", but the compiler wont 
  25. >> issue any warnings, but in:
  26. >> 
  27. >>   fred = (int *)malloc(n * sizeof(int));
  28. >> 
  29. >> the compiler will issue an error.
  30.  
  31. miker3@ix.netcom.com (Mike Rubenstein) replied :
  32.  
  33. >Why will the compiler issue an error in that case?  It's completely
  34. >legal code.
  35.  
  36. From K&R2 section 5.4 :
  37.  
  38.   "It is not legal to [stuff about illegal pointer arithmatic], or even,
  39.    except for void *, to assign a pointer of one type to a pointer of
  40.    another type without a cast."
  41.  
  42. If you assign a int * to a long * the compiler can certainly pick that up.
  43.  
  44. >The error that can be hidden by a cast is omitting the declaration of
  45. >malloc() and not including stdlib.h.  Without the cast a diagnostic is
  46. >required since an int cannot be automatically converted to a pointer.
  47. >With the cast the compiler will convert the int it thinks is returned
  48. >by malloc() to a pointer and few compilers will give any indication of
  49. >the error (read: none that I'm aware of).
  50.  
  51. I've also frequently seen the advice not to cast the return from malloc()
  52. as it supposedly can hide errors, and have followed this advice. However,
  53. having read what Ray Dunn wrote I thought I would try it out on my compiler
  54. (DEC C - with default warnings) to see which cases get picked up.
  55.  
  56. Missing out #include <stdlib.h>, and with a cast on malloc() gives a
  57. warning and information message as follows :
  58.  
  59.         long_ptr = (int *)malloc(sizeof(int));
  60.         ..................^
  61. %CC-I-IMPLICITFUNC, In this statement, the identifier "malloc" is implicitly
  62.  declared as a function.
  63.  
  64.         long_ptr = (int *)malloc(sizeof(int));
  65.         ^
  66. %CC-W-PTRMISMATCH, In this statement, the referenced type of the pointer value
  67.  "(int ...)malloc(...)" is "int", which is not compatible with "long".
  68.  
  69. I would call that fair warning that something is amiss.
  70.  
  71. Including #include <stdlib.h>, and without a cast on malloc() gives no
  72. messages at all on "long_ptr = malloc(sizeof(int));" because a void pointer
  73. (returned from malloc()) is being converted to a long pointer, which is
  74. OK as far as the compiler is concerned.
  75.  
  76. Given the results of this, I think a may start casting the return from
  77. malloc().
  78.  
  79. Phil Tregoning
  80.